package com.example.fill;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

public class Adapter extends ListAdapter<Bean, Adapter.ViewHolder> {


    private static final DiffUtil.ItemCallback<Bean> diffCallback = new DiffUtil.ItemCallback<Bean>() {
        @Override
        public boolean areItemsTheSame(@NonNull Bean oldItem, @NonNull Bean newItem) {
            return oldItem.getId() == newItem.getId();
        }

        @Override
        public boolean areContentsTheSame(@NonNull Bean oldItem, @NonNull Bean newItem) {
            return true;
        }
    };

    public Adapter() {
        super(diffCallback);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_2,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Bean item = getItem(position);
        holder.title.setText(item.getTitle());
        holder.content.setText(item.getContent());
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView title;
        private TextView content;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(android.R.id.text1);
            content = itemView.findViewById(android.R.id.text2);
        }
    }
}
